var selectedRow = '';
var isInit = true;

$(function () {
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openInfoIsvPage();
        },
    }
});

function doRefresh() {
    selectedRow = '';
    var isvName = $('#isvName').val();
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'basic/isvInfo/pageFindIsvData?t=' + new Date().getTime(),
        query: {
            isvName : isvName,
        },
    });
}

function getParams(params) {
    var isvName = $('#isvName').val();
    var pageNo = params.pageNumber;
    var pageSize = params.pageSize;
    if (isInit) {
        isInit = false;
        pageSize = defIsvInfo.pageSize ? defIsvInfo.pageSize : params.pageSize;
        pageNo = defIsvInfo.pageNo ? defIsvInfo.pageNo : params.pageNumber;
    }
    return {
        isvName : isvName,
        pageSize : pageSize,
        pageNo : pageNo
    };
}

function openAddIsvPage() {
    openEditPage('add', 0);
}

function openEditIsvPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("编辑"));
        return;
    }
    openEditPage('edit', selectedRow.id);
}

function openInfoIsvPage() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    openEditPage('info', selectedRow.id);
}

function openEditPage(command, id) {
    var searchKey = $.trim($('#isvName').val());
    var tableOptions = $('#dataTable').bootstrapTable('getOptions', null);
    window.location.href = basePath + 'basic/isvInfo/editIsvPage?type=' + command + '&id=' + id +
        '&searchKey=' + searchKey + '&t=' + new Date().getTime() + '&isIsvBizExist=' + defIsvInfo.isIsvBizExist +
        '&pageNo=' + tableOptions.pageNumber + '&pageSize=' + tableOptions.pageSize;
}

function doDeleteIsv() {
    if (!selectedRow.id || selectedRow.id == '') {
        toastr.error(common_no_choose_msg.format("删除"));
        return;
    }
    var htmlStr = '删除会导致数据库变动，确定要删除【' + selectedRow.isvName + '】吗？';
    swalToastr('', htmlStr, 'warning', deleteIsv);
}

function deleteIsv() {
    var postData = {
        id : selectedRow.id,
        isvPid : selectedRow.isvPid,
        status : 'delete'
    };
    $.ajax({
        type: 'post',
        url: basePath + 'basic/isvInfo/changeState?t=' + new Date().getTime(),
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data && data.isSuccess){
                toastr.success('删除成功');
                doRefresh();
            }else if (data&&data.errorCode == 'EXIST'){
                toastr.error('该服务商关联了业务，无法直接删除');
            } else  if(data&&!data.isSuccess){
                toastr.error(data.errorDesc);
            }else{
                toastr.error('删除失败');
            }
        },
        error: function (request) {
            delayToastr(common_system_error);
        }
    });
}

function doExport() {
    $('#searchForm').attr("action",basePath+"basic/isvInfo/exportIsvDataExcel");
    $('#searchForm').submit();
}

$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $(".success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
});